package com.sponge.utils;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sponge.constant.DatabaseConstant;

import java.sql.Connection;
import java.sql.SQLException;

public class JdbcUtils {

    static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    static {
//        System.out.println("加载JdbcUtils");
            try {
                Class.forName(DatabaseConstant.driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            comboPooledDataSource.setJdbcUrl(DatabaseConstant.mysql_url);
            comboPooledDataSource.setUser(DatabaseConstant.mysql_user);
            comboPooledDataSource.setPassword(DatabaseConstant.mysql_password);
            comboPooledDataSource.setMaxPoolSize(40);
            comboPooledDataSource.setMinPoolSize(2);
            comboPooledDataSource.setInitialPoolSize(10);
            comboPooledDataSource.setMaxStatements(180);
            //最大空闲时间
            comboPooledDataSource.setMaxIdleTime(600);
    }

    public static Connection getConnection() throws SQLException {
        return comboPooledDataSource.getConnection();
    }
}
